package cuckoo.ioc.scanner;

import java.io.IOException;
import java.net.URL;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

import cuckoo.ioc.scanner.impl.FileClassesScanner;
import cuckoo.ioc.scanner.impl.JarClassesScanner;

public class BeanScanner {

	private HashMap<String, ClassesScanner> scannerMap = new HashMap<String, ClassesScanner>();

	public BeanScanner() {
		scannerMap.put("file", new FileClassesScanner());
		scannerMap.put("jar", new JarClassesScanner());
	}

	public Set<Class<?>> doScan(String path) throws IOException {
		Set<Class<?>> classes = new HashSet<Class<?>>();

		Enumeration<URL> resources = Thread.currentThread().getContextClassLoader().getResources(path);
		while (resources.hasMoreElements()) {
			URL url = resources.nextElement();
			classes.addAll(scannerMap.get(url.getProtocol()).doScan(path, url));
		}

		return classes;
	}
}
